package com.bx.implatform.service.impl;

import com.bx.implatform.entity.CallRecord;
import com.bx.implatform.entity.Enterprise;
import com.bx.implatform.mapper.CallRecordMapper;
import com.bx.implatform.service.ICallRecordService;
import com.bx.implatform.service.IEnterpriseService;
import com.bx.implatform.service.IUserService;
import com.bx.implatform.util.BeanUtils;
import com.bx.implatform.vo.CallRecordVo;
import com.bx.implatform.vo.UserVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author: yufeng
 * @date: 2024-02-20
 * @description:
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CallRecordServiceImpl implements ICallRecordService {

    private final CallRecordMapper callRecordMapper;
    private final IUserService userService;
    private final IEnterpriseService enterpriseService;

    @Override
    public void addCallRecord(CallRecordVo callRecordVo) {
        CallRecord callRecord = BeanUtils.copyProperties(callRecordVo, CallRecord.class);
        callRecordMapper.insert(callRecord);
    }

    @Override
    public List<CallRecordVo> queryCallRecord(Long userId, String callStatus) {
        List<CallRecord> callRecords = callRecordMapper.selectCallRecords(userId, callStatus);
        Map<Long, UserVO> userId2Vo = userService.queryUsers().stream().collect(Collectors.toMap(UserVO::getId, u -> u));
        //获取用户企业信息
        Map<String, String> user2Enterprise = enterpriseService.queryEnterprises().stream().collect(Collectors.toMap(Enterprise::getCompanyCode, Enterprise::getCompanyName));
        return callRecords.stream().map(r -> {
            CallRecordVo recordVo = new CallRecordVo();
            BeanUtils.copyProperties(r, recordVo);
            //判断去电/来电
            if (userId.equals(r.getSendId())) {
                recordVo.setCallType("去电");
                //获取对方用户信息
                UserVO vo = userId2Vo.get(r.getReceiveId());
                if (Objects.nonNull(vo)) {
                    //填充姓名&手机号
                    recordVo.setUserName(vo.getNickName());
                    recordVo.setPhoneNum(vo.getPhonenumber());
                    recordVo.setEnterprise(user2Enterprise.get(vo.getCompanyCode()));
                }
            } else {
                recordVo.setCallType("来电");
                //获取对方用户信息
                UserVO vo = userId2Vo.get(r.getSendId());
                if (Objects.nonNull(vo)) {
                    //填充姓名&手机号
                    recordVo.setUserName(vo.getNickName());
                    recordVo.setPhoneNum(vo.getPhonenumber());
                    recordVo.setEnterprise(user2Enterprise.get(vo.getCompanyCode()));
                }
            }
            return recordVo;
        }).collect(Collectors.toList());
    }

    @Override
    public Map<Long, List<CallRecordVo>> queryPersonalCallRecord(Long currentUserId, Long peerUserId) {
        List<CallRecord> callRecords = callRecordMapper.selectPersonalCallRecords(currentUserId, peerUserId);
        List<CallRecordVo> callRecordVos = callRecords.stream().map(callRecord -> {
            CallRecordVo recordVo = new CallRecordVo();
            BeanUtils.copyProperties(callRecord, recordVo);
            if (currentUserId.equals(callRecord.getSendId())) {
                recordVo.setCallType("去电");
            } else {
                recordVo.setCallType("来电");
            }
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date ymdDate = df.parse(df.format(callRecord.getCreatedTime()));
                recordVo.setDateStamp(ymdDate.getTime());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return recordVo;
        }).collect(Collectors.toList());
        return callRecordVos.stream().collect(Collectors.groupingBy(CallRecordVo::getDateStamp));
    }

    @Override
    public void deleteCallRecord(Long id) {
        callRecordMapper.deleteById(id);
    }


}
